package com.bjpowernode.service.impl;

import com.bjpowernode.mapper.ProductInfoMapper;
import com.bjpowernode.pojo.ProductInfo;
import com.bjpowernode.pojo.ProductInfoExample;
import com.bjpowernode.pojo.vo.ProductInfoVo;
import com.bjpowernode.service.ProductInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductInfoServiceImpl implements ProductInfoService {

    @Autowired
    private ProductInfoMapper productInfoMapper;

    @Override
    public List<ProductInfo> getAll() {
        List<ProductInfo> productInfos = productInfoMapper.selectByExample(new ProductInfoExample());
        return productInfos;
    }

    @Override
    public PageInfo splitPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);

        ProductInfoExample example = new ProductInfoExample();
        example.setOrderByClause("p_id desc");
        List<ProductInfo> productInfos = productInfoMapper.selectByExample(example);

        PageInfo<ProductInfo> pageInfo = new PageInfo<>(productInfos);
        return pageInfo;
    }

    @Override
    public int save(ProductInfo info) {
        return productInfoMapper.insert(info);
    }

    @Override
    public ProductInfo getById(int pId) {
        return productInfoMapper.selectByPrimaryKey(pId);
    }

    @Override
    public PageInfo<ProductInfo> splitPageVo(ProductInfoVo vo, int pageSize) {
        // 四个条件的查询：vo.getPage()是1
        // 下面分页的点击：vo.getPage()是”点击的第几页“
        PageHelper.startPage(vo.getPage(), pageSize);
        List<ProductInfo> productInfos = productInfoMapper.selectCondition(vo);
        return new PageInfo<>(productInfos);
    }


    @Override
    public int update(ProductInfo productInfo) {
        return productInfoMapper.updateByPrimaryKey(productInfo);
    }

    @Override
    public PageInfo<ProductInfo> splitbyPage(int page,int PageSize) {
        PageHelper.startPage(page,PageSize);
        List<ProductInfo> productInfos = productInfoMapper.selectAll();
        return new PageInfo(productInfos);
    }

    @Override
    public int delete(int pid) {
        return productInfoMapper.deleteByPrimaryKey(pid);
    }

    @Override
    public ProductInfo selectByPrimaryKey(int pid) {
        return productInfoMapper.selectByPrimaryKey(pid);
    }

    @Override
    public int deleteBatch(String[] split) {
        return productInfoMapper.deleteBatch(split);
    }
}
